package com.eatme.web.security.authentication.password.filter;

import com.eatme.web.security.domain.vo.LoginRequest;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.log.LogMessage;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;

public class RestAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
    private static final Logger LOG = LoggerFactory.getLogger(RestAuthenticationFilter.class);
    private final ObjectMapper objectMapper;

    public RestAuthenticationFilter(ObjectMapper objectMapper) {
        this.objectMapper = objectMapper;
    }

    /**
     * 用户认证 {}
     * @param request
     * @param response
     * @return
     * @throws AuthenticationException
     */
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

        UsernamePasswordAuthenticationToken authRequest;
        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }



        try (InputStream is = request.getInputStream()) {
//  在这里取消原有的通过 request parameter 形式获取用户名密码的方法, 替换为 json 数据格式的获取

//        BufferedReader reader = request.getReader();// 从请求体获取数据
//        StringBuilder sb = new StringBuilder();
//        String line = null;
//        while (line = reader.readLine()!=null) {
//            sb.append(line);
//        }

//        String username = this.obtainUsername(request);
//        username = username != null ? username.trim() : "";
//        String password = this.obtainPassword(request);
//        password = password != null ? password : "";


            LoginRequest authenticationBean = objectMapper.readValue(is, LoginRequest.class);
            authRequest = new UsernamePasswordAuthenticationToken(authenticationBean.getUsername(), authenticationBean.getPassword());

//            Map<String,String> authenticationBean = objectMapper.readValue(is, User.class);
//            authRequest = new UsernamePasswordAuthenticationToken(authenticationBean.get("username"), authenticationBean.get("password"));

//            JsonNode jsonNode = objectMapper.readTree(is);
//            String username = jsonNode.get("username").textValue();
//            String password = jsonNode.get("password").textValue();
//            authRequest = new UsernamePasswordAuthenticationToken(username, password);
        } catch (IOException e) {
            throw new BadCredentialsException("没有找到用户名或密码参数");
        }
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }
}
